package com.ygqh.baby.controller.admin;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import sun.misc.BASE64Decoder;

import com.foxinmy.weixin4j.type.TradeType;
import com.foxinmy.weixin4j.util.StringUtil;
import com.ygqh.baby.ao.CodeType;
import com.ygqh.baby.ao.FileType;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.Message.Type;
import com.ygqh.baby.ao.OrderStatus;
import com.ygqh.baby.ao.OrderType;
import com.ygqh.baby.ao.PayType;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.ao.WdtTradeStatus;
import com.ygqh.baby.controller.utils.DocumentHandler;
import com.ygqh.baby.model.logistics;
import com.ygqh.baby.model.wdt.WdtOrderResData;
import com.ygqh.baby.po.AdminUser;
import com.ygqh.baby.po.KsdOrder;
import com.ygqh.baby.po.YgLogistics;
import com.ygqh.baby.po.YgOrder;
import com.ygqh.baby.service.FileService;
import com.ygqh.baby.service.KsdOrderService;
import com.ygqh.baby.service.YgFrameCodeService;
import com.ygqh.baby.service.YgLogisticsService;
import com.ygqh.baby.service.YgOrderLogService;
import com.ygqh.baby.service.YgOrderPaymentService;
import com.ygqh.baby.service.YgOrderService;
import com.ygqh.baby.service.task.WdtService;
import com.ygqh.baby.service.task.YgAsynClient;
import com.ygqh.baby.service.wdtqyb.api.WdtQybApi;
import com.ygqh.baby.service.wechat.YgWeChatService;
import com.ygqh.baby.service.wechat.protocol.UnifiedOrderReqData;
import com.ygqh.baby.utils.DateConvertUtils;
import com.ygqh.baby.utils.QRCodeUtil;
import com.ygqh.baby.utils.WebUtils;

@Controller
@RequestMapping("/admin/offlineorder")
public class OffLineOrderController extends BaseController<Object, java.lang.Long> {

	@Autowired
	private KsdOrderService ksdOrderService;
	@Autowired
	private FileService fileService;
	
	

	@RequestMapping("searchOrder")
	@ResponseBody
	public Map<String, Object> searchOrder(String q, QueryInfo queryInfo, Date startDate, Date endDate,OrderStatus orderStatus) {

		ResultSet<KsdOrder> resultSet = ksdOrderService.search(queryInfo, q,startDate,endDate,orderStatus);
		return resultSet.toDatatables(null);
	}

	@RequestMapping("orderDetail")
	@ResponseBody
	public List<Map<String,Object>> orderDetail(Long orderId) {

		return ksdOrderService.findOrderDetail(orderId);
	}
	@RequestMapping("orderReport")
	public void orderReport(Date startDate, Date endDate,OrderStatus orderStatus,HttpServletRequest request,HttpServletResponse response){
		endDate = DateConvertUtils.addDay(endDate, 1);
		List<Map<String, Object>> list = ksdOrderService.getOrderReport(startDate, endDate,orderStatus);		
		byte[] bytes = fileService.downloadFromOss("template/offlineorderlist.xls"); 
		List<Map<String, Object>> dataMaps = new ArrayList<Map<String, Object>>();
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("resultSet", list);
		dataMaps.add(dataMap);
		DocumentHandler.DownloadExcel(bytes, "地面店订单统计.xls", dataMap, request, response);
	}

	
}


